// pages/Census/customer-list.js
const Controllers = require('../../controllers/ControllerCensus').controller;
const agentIndex = require('../../controllers/Controller').controller.agentIndex;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        value: 0,
        text: '店铺租金收益',
        key: 'rent',
        status: 8
      },
      {
        value: 0,
        text: '云商品收益',
        key: 'cloud',
        status: 7,
        className: 'fontColor',
      }
    ],
    active: 0,
    shopId: '',
    page: {
      pageNumber: 1,
      pageSize: 5,
      genre: 0
    },
    agentIndexData: {},
    lastPage:false
  },
  agentIndex() {
    var that = this;
    const shopId = that.data.shopId;
    agentIndex({shopId}).then(res => {
      console.log('agentIndex--res==', res);
      const { code, data, desc } = res;
      app.globalData.agentIndexData = data;
      that.setData({
        agentIndexData: data
      })
    })
  },
  agentIncome() {
    var that = this;
    const agentId = that.data.agentId;
    var params = {};
    if (that.data.agentId) {
      params.agentId = agentId;
    }
    Controllers.agentIncome(params).then(res => {
      console.log('agentIncome--res==', res);
      const { code, data, desc } = res;
      app.globalData.agentIndexData = data;
      that.setData({
        agentIndexData: data
      })
    })
  },
  
  agentIncomeDetails() {
    var that = this;
    const shopId = that.data.shopId;
    const tabs = that.data.tabs;
    const active = that.data.active;
    const page = that.data.page;
    wx.showLoading();
    console.log('tabs==', tabs);
    console.log('active==', active);
    const type = tabs[active].status;
    var params = { ...page, type }
    Controllers.agentIncomeDetails(params).then(res => {
      console.log('agentIncomeDetails--res==', res);
      const { code, data, desc } = res;
      const { list, pageNumber } = data;
      if (pageNumber === 1) {
        that.data.list = list;
      } else {
        that.data.list = that.data.list.concat(list);
      }
      that.setData({
        list: that.data.list,
        lastPage:res.data.lastPage
      })
      wx.hideLoading()
    })
  },

  getMore(){
    if(this.data.lastPage==false){
      let num=this.data.page.pageNumber;
      num=num+1;
      this.setData({
        'page.pageNumber':num
      })
      this.agentIncomeDetails()
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      active: parseInt(options.active) || 0,
      agentId: parseInt(options.agentId) || 0
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.agentIncome();
    that.agentIncomeDetails();
  },
  changeTab(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index !== that.data.active) {
      that.data.page.pageNumber = 1;
      that.setData({
        active: index,
        page: that.data.page
      })
      that.agentIncomeDetails();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})